
import 'dart:convert';

import 'package:syria_sports/presentation/exercises/exercises_page/models/exercise.dart';

import '../../core/app_export.dart';
import '../../core/utils/show_toast.dart';
import '../../presentation/tools/tools_page/models/tool.dart';
import '../apiClient/api_client.dart';
import '../apiClient/api_keys.dart';
import '../models/data.dart';
import '../models/error_model.dart';
import '../remote/dio_helper.dart';

class ToolsController{
  static Future<Data?> getAllTools()async{
    var res = await DioHelper.getData( url: ApiClient.getTools, bearerToken: PrefUtils.getToken());

    if(res.statusCode != 200){
      ErrorModel.fromResponse(res);
      return null;
    }else{
      // showMessage(res.data[ApiKeys.message]);
    }
    Data response = Data.fromJson(res.data);
    return response;
  }
  static Future<Data?> getExerciseTools({required String exerciseId})async{
    var res = await DioHelper.getData(
      url: ApiClient.getExerciseTools,
      query: {
        "exercise_id":exerciseId,
      },
      bearerToken: PrefUtils.getToken(),
    );

    if(res.statusCode != 200){
      ErrorModel.fromResponse(res);
      return null;
    }else{
      // showMessage(res.data[ApiKeys.message]);
    }

    Data response = Data.fromJson(res.data);
    return response;
  }

  static Future<Data?> getAllPublicTools()async{
    var res = await DioHelper.getData( url: ApiClient.getPublicTools, bearerToken: PrefUtils.getToken());

    if(res.statusCode != 200){
      ErrorModel.fromResponse(res);
      return null;
    }else{
      // showMessage(res.data[ApiKeys.message]);
    }
    Data response = Data.fromJson(res.data);
    return response;
  }
  static Future<Data?> addTool(ToolModel tool)async{
    var res = await DioHelper.postData(
      url: ApiClient.tool,
      data: await tool.toJson(),
      bearerToken: PrefUtils.getToken(),
    );
    if(res == null)return null;
    if(res.statusCode != 200){
      ErrorModel.fromResponse(res);
    }else{
      showMessage(res.data[ApiKeys.message]);
    }
    return null;
  }

  static Future<Data?> addToExercise({required ToolModel tool,required Exercise exercise})async{
    var res = await DioHelper.postData(
      url: ApiClient.toolAddToExercise,
      data: {
        'tool_id':tool.toolId,
        'exercise_id':exercise.exerciseId,
      },
      bearerToken: PrefUtils.getToken(),
    );
    if(res == null)return null;
    if(res.statusCode != 200){
      ErrorModel.fromResponse(res);
    }else{
      showMessage(res.data[ApiKeys.message]);
    }
    return null;
  }

  static Future<Data?> getToolById(int id)async{
    var res = await DioHelper.getData(
      url: ApiClient.tool,
      query: {
        'id':id
      },
      bearerToken: PrefUtils.getToken(),
    );
    if(res.statusCode != 200){
      ErrorModel.fromResponse(res);
    }else if(!res.data[ApiKeys.state]){
      showMessage(res.data[ApiKeys.message]);
    }else{
      Data response = Data.fromJson(res.data);
      return response;
    }
    return null;
  }

  static Future<Data?> deleteByIds(List<int> ids)async{
    String ids1 = ids.toString().replaceAll('[', '').replaceAll(']', '').replaceAll(' ', '');

    var res = await DioHelper.postData(
      url: ApiClient.exerciseToolsDelete,
      data: {
        'test':"ids",
      },
      query: {
        'ids':ids1,
      },
      bearerToken: PrefUtils.getToken(),
    );

    if(res == null)return null;
    if(res.statusCode != 200){
      //ErrorModel.fromResponse(res);

    }else{
      return Data.fromJson(res.data);
    }

    return null;
  }

  static Future<Data?> updateTool(ToolModel tool)async{
    var res = await DioHelper.postData(
      url: ApiClient.toolUpdate,
      data:await tool.toJson(),
      bearerToken: PrefUtils.getToken(),
    );
    if(res == null)return null;

    if(res.statusCode != 200){
      ErrorModel.fromResponse(res);
    }else{
      showMessage(res.data[ApiKeys.message]);
    }

    return null;
  }

}